﻿Imports VendasBLL
Public Class AgendamentoUC
    Dim ObjBLL As New AgendamentoBLL


#Region "Carrega grid"
    Sub CarregaGrid()
        DGVDados.DataSource = ObjBLL.buscar(FormatDateTime(Date.Now, DateFormat.ShortDate).ToString(), 7)
        DGVDados.Refresh()
    End Sub
#End Region

#Region "Novo"
    Private Sub TSBNovo_Click(sender As System.Object, e As System.EventArgs) Handles TSBNovo.Click
        Try
            AgendamentoFRM.Op = "I"
            TSBNovo.BackColor = Color.Gainsboro
            AgendamentoFRM.ShowDialog()
            CarregaGrid()
        Catch ex As Exception
        Finally
            TSBNovo.BackColor = Color.Transparent
        End Try

    End Sub
#End Region

#Region "Load"
    Private Sub CargosUC_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        CarregaGrid()
        PLocalizar.Visible = False
        'Determinar o começo o filtro como descrição
        CBFiltro.SelectedIndex = 1
        CBFiltro.Focus()
    End Sub
#End Region

#Region "Alterar"
    Private Sub TSBAlterar_Click(sender As System.Object, e As System.EventArgs) Handles TSBAlterar.Click

        Dim indice As Integer = -1
        Try
            For Each linha As DataGridViewRow In DGVDados.SelectedRows
                If (Not linha.Cells("age_codigo") Is Nothing) Then
                    indice = linha.Index
                End If
            Next
            If (indice <> -1) Then
                If ValidaExcluir(DateTime.Parse(DGVDados.Rows(indice).Cells("age_data").Value), DateTime.Parse(DGVDados.Rows(indice).Cells("age_horainicio").Value.ToString()).ToString("HH:mm:ss")) Then
                    AgendamentoFRM.Codigo = Val(DGVDados.Rows(indice).Cells("age_codigo").Value)
                    AgendamentoFRM.Op = "A"
                    TSBAlterar.BackColor = Color.Gainsboro
                    AgendamentoFRM.ShowDialog()
                    CarregaGrid()
                Else : Throw New Exception("Agendamento não pode ser alterado, pois data ou horário já expirou")
                End If
            Else : Throw New Exception("Selecione agendamento a ser realizado á alteração")
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Atenção", MessageBoxButtons.OK, MessageBoxIcon.Information)

        Finally
            TSBAlterar.BackColor = Color.Transparent
        End Try
    End Sub
#End Region

    Function ValidaExcluir(Data As DateTime, Hora As DateTime) As Boolean
        If (Data = Date.Now.Date) And (Hora > DateTime.Now.ToString("HH:mm") Or (Data > Date.Now.Date)) Then Return True
        If (Data > Date.Now.Date) Then Return True
        Return False
    End Function


#Region "Excluir"
    Private Sub TSBExcluir_Click(sender As System.Object, e As System.EventArgs) Handles TSBExcluir.Click
        Try
            Dim cod As Integer = 0
            Dim indice As Integer = -1
            For Each linha As DataGridViewRow In DGVDados.SelectedRows
                If (Not linha.Cells("age_codigo") Is Nothing) Then
                    indice = linha.Index
                    ObjBLL = New AgendamentoBLL(Val(DGVDados.Rows(linha.Index).Cells("age_codigo").Value.ToString()))
                End If
            Next
            If (ObjBLL.Codigo > 0) Then
                TSBExcluir.BackColor = Color.Gainsboro
                If ValidaExcluir(DateTime.Parse(DGVDados.Rows(indice).Cells("age_data").Value), DateTime.Parse(DGVDados.Rows(indice).Cells("age_horainicio").Value.ToString()).ToString("HH:mm:ss")) Then
                    If MessageBox.Show("Confirma exclusão do agendamento Nº " & ObjBLL.Codigo & " ?", "", MessageBoxButtons.YesNo) = vbYes Then
                        ObjBLL.Excluir()
                        CarregaGrid()
                    End If
                Else : Throw New Exception("Agendamento não pode ser excluido, pois data ou horário já expirou")
                End If
            Else : Throw New Exception("Selecione o agendamento a ser excluido")
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Atenção", MessageBoxButtons.OK, MessageBoxIcon.Information)

        Finally
            TSBExcluir.BackColor = Color.Transparent

        End Try
    End Sub
#End Region

#Region "Localizar"
    Private Sub TSBLocalizar_Click(sender As System.Object, e As System.EventArgs) Handles TSBLocalizar.Click
        If PLocalizar.Visible = True Then
            PLocalizar.Visible = False
            TXTLocalizar.Text = ""
            TSBLocalizar.BackColor = Color.Transparent
        Else
            PLocalizar.Visible = True
            TXTLocalizar.Text = ""
            TSBLocalizar.BackColor = Color.Gainsboro
        End If
    End Sub
#End Region

#Region "ComboBox Filtro"
    Private Sub CBFiltro_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles CBFiltro.KeyPress
        e.Handled = True
    End Sub
#End Region

#Region "Localizar no textbox"
    Private Sub TXTLocalizar_TextChanged(sender As System.Object, e As System.EventArgs) Handles TXTLocalizar.TextChanged
        Select Case CBFiltro.Text
            Case "Código"
                DGVDados.DataSource = ObjBLL.buscar(TXTLocalizar.Text, CBFiltro.SelectedIndex)
            Case "Cliente"
                DGVDados.DataSource = ObjBLL.buscar(TXTLocalizar.Text, CBFiltro.SelectedIndex)
            Case "Placa"
                DGVDados.DataSource = ObjBLL.buscar(TXTLocalizar.Text, CBFiltro.SelectedIndex)
            Case "Data"
                BBuscar_Click(sender, e)
            Case "Status"
                DGVDados.DataSource = ObjBLL.buscar(TXTLocalizar.Text, CBFiltro.SelectedIndex)
        End Select
        If TXTLocalizar.Text = "" Then
            DGVDados.DataSource = ObjBLL.buscar(FormatDateTime(Date.Now, DateFormat.ShortDate).ToString(), 7)
        End If
        DGVDados.Refresh()
    End Sub

#End Region

#Region "Voltar"
    Private Sub TSBVoltar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TSBVoltar.Click
        statustabvendas = "N"
        Dispose()
    End Sub
#End Region

#Region "Keypress Localizar"
    Private Sub TXTLocalizar_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TXTLocalizar.KeyPress, TXTLocalizar.KeyPress
        If CBFiltro.SelectedIndex = 0 Then
            If Not Char.IsNumber(e.KeyChar) And Not e.KeyChar = vbBack Then
                e.Handled = True
            End If
        End If
    End Sub
#End Region

#Region "ComboboxFiltro SelectedIndex"
    Private Sub CBFiltro_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles CBFiltro.SelectedIndexChanged
        If CBFiltro.SelectedIndex = 4 Then
            CBStatus.Visible = True
            TXTLocalizar.Visible = False
            CBStatus.SelectedIndex = 0
            CBStatus_SelectedIndexChanged(sender, e)
        Else
            CBStatus.Visible = False
            TXTLocalizar.Visible = True
            CarregaGrid()
        End If
        If CBFiltro.SelectedIndex = 3 Then
            TXTLocalizar.Visible = False
            DTPInicial.Visible = True
            DTPFinal.Visible = True
            LBInformacao.Visible = True
            BBuscar.Visible = True
        Else
            BBuscar.Visible = False
            DTPInicial.Visible = False
            DTPFinal.Visible = False
            LBInformacao.Visible = False
        End If
        TXTLocalizar.Text = ""

    End Sub
#End Region

#Region "ComboboxStatus Selectedindex"
    Private Sub CBStatus_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles CBStatus.SelectedIndexChanged
        DGVDados.DataSource = ObjBLL.buscar(CBStatus.Text, CBFiltro.SelectedIndex)
    End Sub
#End Region

#Region "Buscar por datas"
    Private Sub BBuscar_Click(sender As System.Object, e As System.EventArgs) Handles BBuscar.Click
        Try
            DGVDados.DataSource = ObjBLL.locdata(DTPInicial.Text, DTPFinal.Text)
            DGVDados.Refresh()
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Atenção", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End Try
    End Sub
#End Region

#Region "Cor de fundo do datagridview"
    Private Sub DGVDados_RowPrePaint(sender As System.Object, e As System.Windows.Forms.DataGridViewRowPrePaintEventArgs) Handles DGVDados.RowPrePaint
        If DGVDados.Rows(e.RowIndex).Index Mod (2) = 1 Then
            DGVDados.Rows(e.RowIndex).DefaultCellStyle.BackColor = Color.Gainsboro
        End If
    End Sub
#End Region

End Class
